//基础路径
var base = document.getElementById('testScript').getAttribute('base');

//是否登陆
var isLogin = document.getElementById('testScript').getAttribute('isLogin');
//课程id
var courseId = document.getElementById('testScript').getAttribute('courseId');

// 时间戳转换成时间
function timestampToTime(cjsj) {
    var date = new Date(cjsj); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s
}

//课程收藏
var course = new Vue({
    el: "#banner-info",
    data() {
        return {
            isCollect:0
        }
    },
    methods: {
        //判断用户是否收藏
        getIsCollect: function () {
            var _t = this;
            if (isLogin) {
                axios.get(base + '/course/collect/' + courseId)
                    .then(function (response) {
                        if (response.data.code === 200) {
                            //有收藏该课程
                            _t.isCollect = 1;
                        }
                    })
            }
        },
        //收藏或取消收藏
        collect: function () {
            console.log("点击收藏");
            if (isLogin === 'false') {
                //未登录
                window.location.href = base + '/users/login';
                return;
            }
            var _t = this;

            if (_t.isCollect===1) {
                //取消收藏
                axios.post(base + '/users/collect/false/' + courseId).then(function (response) {
                    if (response.data.code === 200) {
                        //取消收藏成功
                        console.log("取消收藏成功");
                        _t.isCollect = 0;
                    }
                })
            } else {
                //收藏课程
                axios.post(base + '/users/collect/true/' + courseId).then(function (response) {
                    if (response.data.code === 200) {
                        //收藏成功
                        console.log("收藏成功");
                        _t.isCollect = 1;
                    }
                })
            }
        }
    },
    created: function () {
        this.getIsCollect();
    }
});

//问答
var ask = new Vue({
    el: "#ask",
    data() {
        return {
            //页码
            offset: 1,
            //问题列表
            questions: [],
            //章节
            chapters: [],
            footerText: '显示更多问题',
            //按章节id筛选，为0时表示全部
            chapterId: 0,
            //按问题发布者id筛选，为0时为全部，默认只有当前登录用户能选择筛选自己发表的问题
            questionUserId: 0,
            //问题加载等待动画
            loadingQuestion: true

        }
    },
    methods: {
        //加载问题
        getQuestion: function (courseId, limit, offset) {
            var _t = this;

            var data = {
                courseId: courseId,
                limit: limit,
                offset: offset,
                chapterId: this.chapterId,
                userId: this.questionUserId
            };
            if (_t.footerText === '显示更多问题') {
                //加载等待动画
                _t.loadingQuestion = true;
                axios.get(base + "/question/", {
                    params: data
                }).then(function (response) {
                    //结束等待动画
                    _t.loadingQuestion = false;
                    if (response.data.code === 200) {
                        var questions = response.data.data.list;
                        for (var i = 0; i < questions.length; i++) {
                            //添加到问题列表
                            _t.questions.push(questions[i]);
                        }
                    } else if (response.data.code === 400) {
                        if (response.data.message === '查询结果为空') {
                            _t.footerText = '没有更多了！';
                        }
                    }
                });
            }
        },

        //加载更多问题
        getMore: function () {
            //页码+1
            this.offset += 1;
            //加载下一页
            this.getQuestion(courseId, 5, this.offset);
        },
        //加载章节标签
        getChapter: function () {
            var _t = this;
            axios.get(base + "/chapter", {
                params: {
                    courseId: courseId
                }
            }).then(function (response) {
                if (response.data.code === 200) {
                    var chapters = response.data.data;
                    var num = 1;
                    var node = 1;
                    for (var i = 1; i < chapters.length; i++) {
                        if (chapters[i].parent === 0) {//parent===0表示父章节
                            num++;//父章节+1
                            node = 1;//重置子章节
                        } else {//子章节
                            //存入数组
                            _t.chapters.push({'id': chapters[i].id, 'num': num + '.' + node});
                            //子章节+1
                            node++;
                        }
                    }
                }
            });
        },
        //筛选问题
        filterChapter: function (index) {

            if (index === -1) {
                //选择全部
                this.chapterId = 0;
            } else {
                //添加章节条件
                this.chapterId = this.chapters[index].id;
            }
            //清空问题列表
            this.questions = [];
            //重置页码
            this.offset = 1;
            //恢复加载更多
            this.footerText = '显示更多问题';
            //加载筛选后的问题
            this.getQuestion(courseId, 5, 1)
        },
        timestampToTime: function (str) {
            return timestampToTime(str);
        },
        //我有问题按钮
        makeQuestion: function () {
            if (isLogin) {//已登陆用户跳转到发表问题界面
                window.location.href = base + '/question/add/' + courseId;
            } else {//未登录用户跳转到登陆界面
                window.location.href = base + '/users/login';
            }
        },
        //查看问题详情
        getQuestionInfo: function (questionId) {
            window.open(base + '/question/' + questionId);
        }
    },
    created: function () {
        //首次加载问题列表
        this.getQuestion(courseId, 2, 1);
        //获取章节标签
        this.getChapter();
    }
});

//评论
var comment = new Vue({
    el: "#comment",
    data() {
        return {
            //评论输入框内容
            content: '',
            comments: [],
            footerText: '查看更多评论',
            timeNow: (new Date()).getTime(),
            isBottom: false,
            offset: 1

        }
    },
    methods: {
        //加载评论
        findCommends: function (courseId, limit, offset) {
            // noinspection JSAnnotator
            console.log("加载评论");
            var _t = this;
            if (!this.isBottom) {
                axios.get(base + "/comment", {
                    params: {
                        courseId: courseId,
                        limit: limit,
                        offset: offset
                    }
                }).then(function (response) {
                    var jsonObj = JSON.parse(JSON.stringify(response.data.data));
                    //console.log(jsonObj);
                    var length = jsonObj.length;
                    if (length === 0) {
                        console.log("没有更多评论");
                        _t.footerText = '没有更多了';
                        _t.isBottom = true;
                    } else {
                        console.log("加载评论成功");
                        for (var i = 0; i < length; i++) {
                            //console.log(jsonObj[i]);
                            _t.comments.push(jsonObj[i]);
                        }
                    }
                })
            }

        },
        //加载更多评论
        getMore: function () {
            this.offset += 1;
            this.findCommends(courseId, 4, this.offset);
        },

        //发表评论
        addComment: function () {
            //获取评分
            var scope = $("#scope").html();
            console.log(courseId + scope + this.content);
            if (this.content !== '') {
                //提交评论
                var _t = this;
                axios.post(base + "/comment/", {
                    courseId: courseId,
                    scope: scope,
                    content: this.content
                }).then(function (response) {
                    if (response.data.code === 200) {
                        //评论成功
                        //清空评论输入框
                        console.log("评论成功");
                        _t.content = '';
                        //刷新评论列表
                        _t.comments = [];
                        _t.offset = 1;
                        _t.isBottom = false;
                        _t.findCommends(courseId, 5, _t.offset);
                    }
                });
            }
        },
        timestampToTime: function (str) {
            return timestampToTime(str);
        }

    },


    //首次加载评论
    created() {
        this.findCommends(courseId, 4, this.offset);
    }
});

